

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>Matrix Formatting &mdash; CVXOPT User&#39;s Guide</title>
  

  
  
  
  

  
  <script type="text/javascript" src="_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
        <script src="_static/jquery.js"></script>
        <script src="_static/underscore.js"></script>
        <script src="_static/doctools.js"></script>
        <script src="_static/language_data.js"></script>
    
    <script type="text/javascript" src="_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="copyright" title="Copyright" href="copyright.html" />
    <link rel="prev" title="C API" href="c-api.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="index.html" class="icon icon-home"> CVXOPT User's Guide
          

          
          </a>

          
            
            
              <div class="version">
                1.2.5
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <ul class="current">
<li class="toctree-l1"><a class="reference internal" href="copyright.html">Copyright and License</a></li>
<li class="toctree-l1"><a class="reference internal" href="intro.html">Introduction</a></li>
<li class="toctree-l1"><a class="reference internal" href="matrices.html">Dense and Sparse Matrices</a></li>
<li class="toctree-l1"><a class="reference internal" href="blas.html">The BLAS Interface</a></li>
<li class="toctree-l1"><a class="reference internal" href="lapack.html">The LAPACK Interface</a></li>
<li class="toctree-l1"><a class="reference internal" href="fftw.html">Discrete Transforms</a></li>
<li class="toctree-l1"><a class="reference internal" href="spsolvers.html">Sparse Linear Equations</a></li>
<li class="toctree-l1"><a class="reference internal" href="coneprog.html">Cone Programming</a></li>
<li class="toctree-l1"><a class="reference internal" href="solvers.html">Nonlinear Convex Optimization</a></li>
<li class="toctree-l1"><a class="reference internal" href="modeling.html">Modeling</a></li>
<li class="toctree-l1"><a class="reference internal" href="c-api.html">C API</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">Matrix Formatting</a></li>
<li class="toctree-l1"><a class="reference external" href="http://cvxopt.org">cvxopt.org</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="index.html">CVXOPT User's Guide</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="index.html">Docs</a> &raquo;</li>
        
      <li>Matrix Formatting</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
            
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <div class="section" id="matrix-formatting">
<span id="c-printing"></span><h1>Matrix Formatting<a class="headerlink" href="#matrix-formatting" title="Permalink to this headline">¶</a></h1>
<p>This appendix describes ways to customize the formatting of CVXOPT matrices.</p>
<p>As with other Python objects, the functions <code class="xref py py-func docutils literal notranslate"><span class="pre">repr</span></code> and <code class="xref py py-func docutils literal notranslate"><span class="pre">str</span></code>
return strings with printable representations of matrices.  The command
‘<code class="docutils literal notranslate"><span class="pre">print</span> <span class="pre">A</span></code>’ executes ‘<code class="docutils literal notranslate"><span class="pre">str(A)</span></code>’, whereas the command ‘<code class="docutils literal notranslate"><span class="pre">A</span></code>’
calls ‘<code class="docutils literal notranslate"><span class="pre">repr(A)</span></code>’.  The following example illustrates the default
formatting of dense matrices.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">cvxopt</span> <span class="kn">import</span> <span class="n">matrix</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">A</span> <span class="o">=</span> <span class="n">matrix</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">50</span><span class="p">),</span> <span class="p">(</span><span class="mi">5</span><span class="p">,</span><span class="mi">10</span><span class="p">),</span> <span class="s1">&#39;d&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">A</span>
<span class="go">&lt;5x10 matrix, tc=&#39;d&#39;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">A</span><span class="p">)</span>
<span class="go">[ 0.00e+00  5.00e+00  1.00e+01  1.50e+01  2.00e+01  2.50e+01  3.00e+01 ... ]</span>
<span class="go">[ 1.00e+00  6.00e+00  1.10e+01  1.60e+01  2.10e+01  2.60e+01  3.10e+01 ... ]</span>
<span class="go">[ 2.00e+00  7.00e+00  1.20e+01  1.70e+01  2.20e+01  2.70e+01  3.20e+01 ... ]</span>
<span class="go">[ 3.00e+00  8.00e+00  1.30e+01  1.80e+01  2.30e+01  2.80e+01  3.30e+01 ... ]</span>
<span class="go">[ 4.00e+00  9.00e+00  1.40e+01  1.90e+01  2.40e+01  2.90e+01  3.40e+01 ... ]</span>
</pre></div>
</div>
<p>The format is parameterized by the dictionary <code class="xref py py-data docutils literal notranslate"><span class="pre">options</span></code> in the
module <code class="xref py py-mod docutils literal notranslate"><span class="pre">cvxopt.printing</span></code>.  The parameters <code class="xref py py-attr docutils literal notranslate"><span class="pre">options['iformat']</span></code>
and <code class="xref py py-attr docutils literal notranslate"><span class="pre">options['dformat']</span></code> determine, respectively, how integer and
double/complex numbers are printed.  The entries are Python format strings
with default values <code class="xref py py-const docutils literal notranslate"><span class="pre">'%</span> <span class="pre">.2e'</span></code> for <code class="xref py py-const docutils literal notranslate"><span class="pre">'d'</span></code> and <code class="xref py py-const docutils literal notranslate"><span class="pre">'z'</span></code>
matrices and <code class="xref py py-const docutils literal notranslate"><span class="pre">%</span> <span class="pre">i'</span></code> for <code class="xref py py-const docutils literal notranslate"><span class="pre">'i'</span></code> matrices.  The parameters
<code class="xref py py-attr docutils literal notranslate"><span class="pre">options['width']</span></code> and <code class="xref py py-attr docutils literal notranslate"><span class="pre">options['height']</span></code> specify the maximum
number of columns and rows that are shown.  If <code class="xref py py-attr docutils literal notranslate"><span class="pre">options['width']</span></code> is
set to a negative value, all columns are displayed.  If
<code class="xref py py-attr docutils literal notranslate"><span class="pre">options['height']</span></code> is set to a negative value, all rows are
displayed.  The default values of <code class="xref py py-attr docutils literal notranslate"><span class="pre">options['width']</span></code> and
<code class="xref py py-attr docutils literal notranslate"><span class="pre">options['height']</span></code> are 7 and -1, respectively.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">cvxopt</span> <span class="kn">import</span> <span class="n">printing</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">printing</span><span class="o">.</span><span class="n">options</span>
<span class="go">{&#39;width&#39;: 7, &#39;dformat&#39;: &#39;% .2e&#39;, &#39;iformat&#39;: &#39;% i&#39;, &#39;height&#39;: -1}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">printing</span><span class="o">.</span><span class="n">options</span><span class="p">[</span><span class="s1">&#39;dformat&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">%.1f</span><span class="s1">&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">printing</span><span class="o">.</span><span class="n">options</span><span class="p">[</span><span class="s1">&#39;width&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">A</span><span class="p">)</span>
<span class="go">[ 0.0  5.0 10.0 15.0 20.0 25.0 30.0 35.0 40.0 45.0]</span>
<span class="go">[ 1.0  6.0 11.0 16.0 21.0 26.0 31.0 36.0 41.0 46.0]</span>
<span class="go">[ 2.0  7.0 12.0 17.0 22.0 27.0 32.0 37.0 42.0 47.0]</span>
<span class="go">[ 3.0  8.0 13.0 18.0 23.0 28.0 33.0 38.0 43.0 48.0]</span>
<span class="go">[ 4.0  9.0 14.0 19.0 24.0 29.0 34.0 39.0 44.0 49.0]</span>
</pre></div>
</div>
<p>In order to make the built-in Python functions <code class="xref py py-func docutils literal notranslate"><span class="pre">repr</span></code> and <code class="xref py py-func docutils literal notranslate"><span class="pre">str</span></code>
accessible for further customization, two functions are provided in
CVXOPT.  The function <code class="xref py py-func docutils literal notranslate"><span class="pre">cvxopt.matrix_repr</span></code> is used when
<code class="xref py py-func docutils literal notranslate"><span class="pre">repr</span></code> is called with a matrix argument; and
<code class="xref py py-func docutils literal notranslate"><span class="pre">cvxopt.matrix_str</span></code> is used when <code class="xref py py-func docutils literal notranslate"><span class="pre">str</span></code> is called with a matrix
argument.  By default, the functions are set to
<code class="xref py py-func docutils literal notranslate"><span class="pre">printing.matrix_repr_default</span></code> and
<code class="xref py py-func docutils literal notranslate"><span class="pre">printing.matrix_str_default</span></code>, respectively, but they can be
redefined to any other Python functions.  For example, if we prefer
<code class="docutils literal notranslate"><span class="pre">A</span></code> to return the same output as <code class="docutils literal notranslate"><span class="pre">print</span> <span class="pre">A</span></code>, we can simply
redefine <code class="xref py py-func docutils literal notranslate"><span class="pre">cvxopt.matrix_repr</span></code> as shown below.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">cvxopt</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">cvxopt</span> <span class="kn">import</span> <span class="n">matrix</span><span class="p">,</span> <span class="n">printing</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">A</span> <span class="o">=</span> <span class="n">matrix</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">4</span><span class="p">),</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="mi">2</span><span class="p">),</span> <span class="s1">&#39;d&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">A</span>
<span class="go">&lt;2x2 matrix, tc=&#39;d&#39;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cvxopt</span><span class="o">.</span><span class="n">matrix_repr</span> <span class="o">=</span> <span class="n">printing</span><span class="o">.</span><span class="n">matrix_str_default</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">A</span>
<span class="go">[ 0.00e+00  2.00e+00]</span>
<span class="go">[ 1.00e+00  3.00e+00]</span>
</pre></div>
</div>
<p>The formatting for sparse matrices is similar.  The functions <code class="xref py py-func docutils literal notranslate"><span class="pre">repr</span></code>
and <code class="xref py py-func docutils literal notranslate"><span class="pre">str</span></code> for sparse matrices are <code class="xref py py-func docutils literal notranslate"><span class="pre">cvxopt.spmatrix_repr</span></code>
and <code class="xref py py-func docutils literal notranslate"><span class="pre">cvxopt.spmatrix_str</span></code>, respectively.  By default, they are set to
<code class="xref py py-func docutils literal notranslate"><span class="pre">printing.spmatrix_repr_default</span></code> and
<code class="xref py py-func docutils literal notranslate"><span class="pre">printing.spmatrix_repr_str</span></code>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">cvxopt</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">cvxopt</span> <span class="kn">import</span> <span class="n">printing</span><span class="p">,</span> <span class="n">spmatrix</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">A</span> <span class="o">=</span> <span class="n">spmatrix</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">5</span><span class="p">),</span> <span class="nb">range</span><span class="p">(</span><span class="mi">5</span><span class="p">),</span> <span class="nb">range</span><span class="p">(</span><span class="mi">5</span><span class="p">),</span> <span class="p">(</span><span class="mi">5</span><span class="p">,</span><span class="mi">10</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">A</span>
<span class="go">&lt;5x10 sparse matrix, tc=&#39;d&#39;, nnz=5&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">A</span><span class="p">)</span>
<span class="go">[ 0.00e+00     0         0         0         0         0         0     ... ]</span>
<span class="go">[    0      1.00e+00     0         0         0         0         0     ... ]</span>
<span class="go">[    0         0      2.00e+00     0         0         0         0     ... ]</span>
<span class="go">[    0         0         0      3.00e+00     0         0         0     ... ]</span>
<span class="go">[    0         0         0         0      4.00e+00     0         0     ... ]</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">cvxopt</span><span class="o">.</span><span class="n">spmatrix_repr</span> <span class="o">=</span> <span class="n">printing</span><span class="o">.</span><span class="n">spmatrix_str_default</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">A</span>
<span class="go">[ 0.00e+00     0         0         0         0         0         0     ... ]</span>
<span class="go">[    0      1.00e+00     0         0         0         0         0     ... ]</span>
<span class="go">[    0         0      2.00e+00     0         0         0         0     ... ]</span>
<span class="go">[    0         0         0      3.00e+00     0         0         0     ... ]</span>
<span class="go">[    0         0         0         0      4.00e+00     0         0     ... ]</span>
</pre></div>
</div>
<p>As can be seen from the example, the default behaviour is to print the
entire matrix including structural zeros. An alternative triplet printing
style is defined in <code class="xref py py-func docutils literal notranslate"><span class="pre">printing.spmatrix_str_triplet</span></code>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">cvxopt</span><span class="o">.</span><span class="n">spmatrix_str</span> <span class="o">=</span> <span class="n">printing</span><span class="o">.</span><span class="n">spmatrix_str_triplet</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">A</span><span class="p">)</span>
<span class="go">(0,0)  0.00e+00</span>
<span class="go">(1,1)  1.00e+00</span>
<span class="go">(2,2)  2.00e+00</span>
<span class="go">(3,3)  3.00e+00</span>
<span class="go">(4,4)  4.00e+00</span>
</pre></div>
</div>
</div>


           </div>
           
          </div>
          <footer>
  
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
      
        <a href="c-api.html" class="btn btn-neutral float-left" title="C API" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
      
    </div>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; <a href="copyright.html">Copyright</a> 2004-2020, M.S. Andersen, J. Dahl, L. Vandenberghe
      <span class="lastupdated">
        Last updated on Apr 16, 2020.
      </span>

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
  
    <div class="footer">
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 3.0.1.
    </div>

</body>
</html>